package com.renge.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.commonutils.JwtUtils;
import com.renge.commonutils.MD5;
import com.renge.commonutils.RandomUtil;
import com.renge.exception.GuliException;
import com.renge.ucenterservice.entity.UcenterMember;
import com.renge.ucenterservice.entity.vo.LoginVo;
import com.renge.ucenterservice.entity.vo.RegisterVo;
import com.renge.ucenterservice.mapper.UcenterMemberMapper;
import com.renge.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-20
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;
    @Override
    //登录返回token
    public String loginToToken(LoginVo loginVo) {
        //电话或者密码判空
        if(StringUtils.isEmpty(loginVo)||StringUtils.isEmpty(loginVo.getMobile())
                ||StringUtils.isEmpty(loginVo.getPassword())){
            throw new GuliException(20001,"电话或者密码不能为空");
        }
        //根据电话查询用户
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",loginVo.getMobile());
        UcenterMember ucenterMember = this.getOne(wrapper);
        //判断用户存在
        if(StringUtils.isEmpty(ucenterMember)){
            throw new GuliException(20001,"不存在此用户");
        }
        //判断密码
        String encrypt = MD5.encrypt(loginVo.getPassword());
        if (!encrypt.equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        //判断是否被禁用
        Boolean isDisabled = ucenterMember.getIsDisabled();
        if (isDisabled){
            throw new GuliException(20001,"此用户禁止使用");
        }
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return token;
    }
    //注册
    @Transactional
    @Override
    public boolean saveRegister(RegisterVo registerVo) {
        //判空
        if(StringUtils.isEmpty(registerVo)||StringUtils.isEmpty(registerVo.getCode())
                ||StringUtils.isEmpty(registerVo.getMobile())
                ||StringUtils.isEmpty(registerVo.getNickname())
                ||StringUtils.isEmpty(registerVo.getPassword())){
            throw new GuliException(20001,"未填写完整表格");
        }
        //判定验证码
        String code = redisTemplate.opsForValue().get(registerVo.getMobile());
        if(StringUtils.isEmpty(code)||!code.equals(registerVo.getCode())){
            throw new GuliException(20001,"验证码过期或者错误");
        }
        //录入ucenterMember对象
        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVo,ucenterMember);
        ucenterMember.setPassword(MD5.encrypt(registerVo.getPassword()));
        return this.save(ucenterMember);
    }
     //生产验证码
    @Override
    public String phoneByCode(String number) {
        //判断手机号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",number);
        int count = this.count(wrapper);
        if(count>0){
            throw new GuliException(20001,"改手机号已存在，请登录");
        }
        String fourBitRandom = RandomUtil.getFourBitRandom();
        redisTemplate.opsForValue().set(number,fourBitRandom,5, TimeUnit.MINUTES);
        return fourBitRandom;
    }
    //通过token获得用户
    @Override
    public UcenterMember getUserByToken(HttpServletRequest request) {
        //判断token是否有效
         if(!JwtUtils.checkToken(request)){
             throw new GuliException(20001,"token获取信息失败！");
         }
        String ucenterMemberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = this.getById(ucenterMemberId);
        if(ucenterMember==null){
            throw new GuliException(20001,"token失效，请重新登录");
        }
        //清楚密码信息
        ucenterMember.setPassword(null);
        return ucenterMember;
    }
    //统计每日注册人数
    @Override
    public Integer checkDayByCode(String day) {
        return ucenterMemberMapper.checkDayByCode(day);
    }

}
